<script setup lang="ts">
import { ref } from "vue";
import InstructForm from "./instructForm.vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useSession } from "./hook";

import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import ArrowDownLine from "@iconify-icons/ri/arrow-down-line";
import Scissors2Line from "@iconify-icons/ri/scissors-2-line";
import ScissorsCutLine from "@iconify-icons/ri/scissors-cut-line";
import Message2Line from "@iconify-icons/ri/message-2-line";
import MoreFill from "@iconify-icons/ri/more-fill";
import EyeLine from "@iconify-icons/ri/eye-line";
import PulseLine from "@iconify-icons/ri/pulse-line";
defineOptions({ name: "Session" });

const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  clickLogoffSession,
  clickBreakSession,
  clickSendInstruct,
  clickMonitorBtn,
  clickRecoverBtn,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useSession();
</script>

<template>
  <div>
    <InstructForm />
    <div class="main">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="bg-bg_color w-[99/100] pl-8 pt-4"
      >
        <el-form-item label="主机IP：" prop="hostIp">
          <el-input
            v-model="form.hostIp"
            placeholder="请输入部门名称"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="会话状态：" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择状态"
            clearable
            class="!w-[180px]"
          >
            <el-option label="全部" value="" />
            <el-option label="初始化" value="no_connect" />
            <el-option label="使用中" value="connecting" />
            <el-option label="断开" value="break_connect" />
            <el-option label="注销" value="disconnected" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon(Search)"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <PureTableBar title="用户管理" @refresh="onSearch">
        <template #buttons>
          <el-button type="primary" :icon="useRenderIcon(ArrowDownLine)">
            导出报表
          </el-button>
        </template>
        <template v-slot="{ size, checkList }">
          <pure-table
            border
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-table-row-hover-bg-color)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="clickSendInstruct(row)"
                :icon="useRenderIcon(Message2Line)"
                :disabled="row.holdStatus != 1"
              >
                指令
              </el-button>
              <el-popconfirm
                title="注销会话后不能恢复，请确认?"
                @confirm="clickLogoffSession(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(ScissorsCutLine)"
                    :disabled="row.holdStatus != 1"
                  >
                    注销
                  </el-button>
                </template>
              </el-popconfirm>
              <el-dropdown>
                <el-button
                  class="ml-3"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(MoreFill)"
                />
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>
                      <el-button
                        class="reset-margin"
                        link
                        type="primary"
                        :size="size"
                        @click="clickBreakSession(row)"
                        :icon="useRenderIcon(Scissors2Line)"
                        :disabled="row.holdStatus != 1"
                      >
                        断开
                      </el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        class="reset-margin !h-20px !text-gray-500"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(EyeLine)"
                        @click="clickMonitorBtn(row)"
                      >
                        监视
                      </el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        class="reset-margin !h-20px !text-gray-500"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(PulseLine)"
                        @click="clickRecoverBtn(row)"
                      >
                        恢复
                      </el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped></style>
